package com.menggudf;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.BaseFont;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author：wangdi
 * @Date：2023/6/9 14:58
 * @Des: HtmlToPdfUtil 转换pdf工具类
 */
public class HtmlToPDFUtil {


    /**
     * 根据html生成pdf的base64格式
     *
     * @param html
     * @return
     */
    public static String getPDFBase64ByHtml(String html) throws DocumentException, IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();//构建字节输出流
        ITextRenderer renderer = new ITextRenderer();
        ITextFontResolver fontResolver = renderer.getFontResolver();
        //指定文件字体添加到PDF库，指定字体不作为内部字体，而是外部字体被加载
        fontResolver.addFont("fonts/simSun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        renderer.setDocumentFromString(html);
        renderer.layout();
        renderer.createPDF(baos);
        return new BASE64Encoder().encode(baos.toByteArray());
    }


    /**
     * 根据pdf的base64格式和路径生成pdf文件
     *
     * @param base64 pdf的base64格式
     * @param path   生成pdf的路径
     * @return
     */
    public static String base64ToPDF(String base64, String path) {


        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String fileAdd = sdf.format(new Date());
        //先判断文件是否存在
        path = path + "/" + fileAdd;
        String fileName = path + "/" + System.currentTimeMillis() + ".pdf";//新的文件名

        BufferedInputStream bin = null;
        FileOutputStream fout = null;
        BufferedOutputStream bout = null;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            byte[] bytes = decoder.decodeBuffer(base64);

            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            // 创建从底层输入流中读取数据的缓冲输入流对象
            bin = new BufferedInputStream(bais);

            //获取文件夹路径
            File file = new File(path);
            //如果文件夹不存在则创建
            if (!file.exists() && !file.isDirectory()) {
                file.mkdirs();
            }
            // 创建到指定文件的输出流
            fout = new FileOutputStream(fileName);
            // 为文件输出流对接缓冲输出流对象
            bout = new BufferedOutputStream(fout);
            byte[] buffers = new byte[1024];
            int len = bin.read(buffers);
            while (len != -1) {
                bout.write(buffers, 0, len);
                len = bin.read(buffers);
            }
            // 刷新此输出流并强制写出所有缓冲的输出字节，必须这行代码，否则有可能有问题
            bout.flush();
            //返回存储的路径
            return fileName;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bin.close();
                fout.close();
                bout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }


    public static void main(String[] args) throws DocumentException, IOException {
        String str = "<html><div style='position: relative; margin-top: 25px; height: 800px;page-break-after: always;margin:0;padding:0;'><table style=\"font-family:arial;\">\n" +
                "   <tr>\n" +
                "        <td width=\"0px\">&nbsp;</td>\n" +
                "        <td valign=\"top\" width=\"626px\">\n" +
                "           <div style=\"position:absolute;\">\n" +
                "   \n" +
                "      <span style='left:356px;top:34px;position:absolute;'><img\n" +
                "               src='%3D'/></span>\n" +
                "\n" +
                " <span style='left:100px;top:0;position:absolute;font-size:12px;'><b>Responsible Delivery - CO<sub>2</sub> Neutral</b></span>\n" +
                " <span style='left:196px;top:88px;position:absolute;font-size:8px;'>Sender</span>\n" +
                " <span style='left:196px;top:96px;position:absolute;font-size:8px;'></span>\n" +
                " <span style='left:196px;top:105px;position:absolute;font-size:8px;'></span>\n" +
                " <span style='left:196px;top:114px;position:absolute;font-size:8px;'></span>\n" +
                " <span style='left:196px;top:123px;position:absolute;font-size:8px;'></span>\n" +
                "<span style='left:196px;top:132px;position:absolute;font-size:8px;'></span>\n" +
                " <span style='left:196px;top:141px;position:absolute;font-size:8px;'></span>\n" +
                "<span style='left:242px;top:145px;position:absolute;font-size:8px;'>Phone:</span>\n" +
                "<span style='left:250px;top:88px;position:absolute;font-size:8px;'>Account:</span>\n" +
                " \n" +
                "   <span style='left:4px;top:19px;position:absolute;font-size:12px;'>AMAZON FULFILLMENT CENTER</span>\n" +
                "<span style='left:4px;top:30px;position:absolute;font-size:12px;'>PLOT 1, LYONS PARK</span>\n" +
                "<span style='left:4px;top:41px;position:absolute;font-size:12px;'></span>\n" +
                " <span style='left:4px;top:52px;position:absolute;font-size:12px;'>SAYER DR COVENTRY</span>\n" +
                " <span style='left:4px;top:63px;position:absolute;font-size:12px;'></span>\n" +
                " <span style='left:4px;top:74px;position:absolute;font-size:12px;'>CV5 9PF</span>\n" +
                " <span style='left:90px;top:74px;position:absolute;font-size:12px;'>United Kingdom</span>\n" +
                "<span style='left:220px;top:68px;position:absolute;font-size:18px;'></span>\n" +
                " <span style='left:4px;top:88px;position:absolute;font-size:8px;'>Contact</span>\n" +
                "2023-11-29T16:01:41.361277969Z <span style='left:4px;top:120px;position:absolute;font-size:8px;'>Consignment</span>\n" +
                "2023-11-29T16:01:41.361280494Z <span style='left:4px;top:128px;position:absolute;font-size:8px;'>Ref</span>\n" +
                "2023-11-29T16:01:41.361282986Z <span style='left:4px;top:104px;position:absolute;font-size:8px;'>Info</span>\n" +
                "\n" +
                "    <span style='left:60px;top:104px;font-size:8px;position:absolute;'></span>\n" +
                "  <span style='left:60px;top:112px;font-size:8px;position:absolute;'></span>\n" +
                "\n" +
                " <span style='left:60px;top:88px;position:absolute;font-size:8px;'></span>\n" +
                " <span style='left:60px;top:120px;position:absolute;font-size:8px;'>1862662333</span>\n" +
                "<span style='left:60px;top:128px;position:absolute;font-size:8px;'></span>\n" +
                " <span style='left:60px;top:136px;position:absolute;font-size:8px;'></span>\n" +
                " <span style='left:60px;top:144px;position:absolute;font-size:8px;'></span>\n" +
                " \n" +
                "<span style='left:284px;top:18px;position:absolute;font-size:8px;'>Packages</span>\n" +
                "<span style='left:276px;top:48px;position:absolute;font-size:8px;'>Total Weight</span>\n" +
                "<span style='left:274px;top:26px;position:absolute;font-size:16px;'>1 of 2</span>\n" +
                "<span style='left:274px;top:56px;position:absolute;font-size:16px;'>2 kg</span>\n" +
                "\n" +
                "<span style='left:6px;top:180px;position:absolute;font-size:8px;'>Track</span>\n" +
                "<span style='left:350px;top:180px;position:absolute;font-size:8px;'>Service</span>\n" +
                "<span style='left:5px;top:158px;position:absolute;font-size:22px;'>1550</span>\n" +
                "<span style='left:62px;top:160px;position:absolute;font-size:16px;'>1862 6623 33F</span>\n" +
                "<span style='left:5px;width:370px;top:250px;position:absolute;font-size:10px;text-align:center;'>      29/11/23 16:01 Web 4.6.10-api      </span>\n" +
                "<span style='left:19px;top:265px;border-top-style:solid;border-width:thin;position:absolute;'><img width='348px' height='95px' src=''/></span>\n" +
                "<span style='left:85px;top:363px;position:absolute;font-size:12px;'>0CV5 9PF1 5501 8626 6233 3812 826W</span>\n" +
                "<span style='right:5px;top:158px;width:250px;text-align:right;position:absolute;font-size:20px;'>          NEXT DAY</span>\n" +
                "<span style='left:130px;top:230px;position:absolute;font-size:14px;'>   812-GB - CV5 9PF   </span>\n" +
                "<span style='left:10px;top:190px;position:absolute;font-size:18px;'></span>\n" +
                "<span style='left:10px;top:225px;position:absolute;font-size:30px;'></span>\n" +
                "<span style='right:10px;top:225px;position:absolute;font-size:30px;'>0065</span>\n" +
                " <span style='left:3px;top:183px;width:380px;font-size:46px;position:absolute;text-align:center;'>GB-CV5 9PF</span>\n" +
                " \n" +
                "                <table style='display:inline-block;border:3px solid black;padding:0;border-spacing:0;border-collapse:collapse;' height='385' width='385px'>\n" +
                "   <tr>\n" +
                "      <td>\n" +
                "         <table style='padding:0;border-spacing:0;border-collapse:collapse;' width='100%'>\n" +
                "              <tr>\n" +
                "                 <td style='font-size:1px;border-bottom:1px solid black;' colspan='4'>\n" +
                "                      <div style='height:10px'>&nbsp;</div>\n" +
                "                 </td>\n" +
                "                </tr>\n" +
                "                <tr>\n" +
                "                 <td colspan='2' style='text-align:left;border-bottom:1px solid black;vertical-align:top;'>\n" +
                "                       <div style='height:69px'>&nbsp;</div>\n" +
                "                    </td>\n" +
                "                    <td style='border-bottom:1px solid black;border-left:1px solid black;'>&nbsp;</td>\n" +
                "                 <td style='border-bottom:3px solid black;border-left:1px solid black;' rowspan='2'>&nbsp;</td>\n" +
                "                </tr>\n" +
                "               <tr>\n" +
                "                    <td style='border-bottom:3px solid black;'>\n" +
                "                        <div style='height:66px'>&nbsp;</div>\n" +
                "                   </td>\n" +
                " <td style='border-bottom:3px solid black;border-left:1px solid black;' colspan='2'>&nbsp;</td>\n" +
                "   </tr>\n" +
                " <tr>\n" +
                "<td style='text-align:left;vertical-align:top;' colspan='4'>\n" +
                "   <div style='height:226px'>&nbsp;</div>\n" +
                "      </td>\n" +
                "    </tr>\n" +
                "         <tr>\n" +
                "           <td style='font-size:1px;' width='195px'>&nbsp;</td>\n" +
                "                  <td style='font-size:1px;' width='70px'>\n" +
                "                   <div style='height:1px'>&nbsp;</div>\n" +
                "           </td>\n" +
                "      <td style='font-size:1px;' width='80px'>&nbsp;</td>\n" +
                "                <td style='font-size:1px;' width='40px'>&nbsp;</td>\n" +
                "               </tr>\n" +
                "           </table>\n" +
                "       </td>\n" +
                "   </tr>\n" +
                "</table>\n" +
                "\n" +
                " </div>\n" +
                "        </td>\n" +
                "  </tr>\n" +
                "</table></div><div style='position: relative; margin-top: 25px; height: 800px;page-break-after: always;margin:0;padding:0;'><table style=\"font-family:arial;\">\n" +
                "    <tr>\n" +
                "       <td width=\"0px\">&nbsp;</td>\n" +
                "        <td valign=\"top\" width=\"626px\">\n" +
                "            <div style=\"position:absolute;\">\n" +
                "    \n" +
                "      <span style='left:356px;top:34px;position:absolute;'><img\n" +
                "                src='%3D'/></span>\n" +
                "\n" +
                " <span style='left:100px;top:0;position:absolute;font-size:12px;'><b>Responsible Delivery - CO<sub>2</sub> Neutral</b></span>\n" +
                "<span style='left:196px;top:88px;position:absolute;font-size:8px;'>Sender</span>\n" +
                "<span style='left:196px;top:96px;position:absolute;font-size:8px;'></span>\n" +
                "<span style='left:196px;top:105px;position:absolute;font-size:8px;'></span>\n" +
                "<span style='left:196px;top:114px;position:absolute;font-size:8px;'></span>\n" +
                "<span style='left:196px;top:123px;position:absolute;font-size:8px;'></span>\n" +
                "<span style='left:196px;top:132px;position:absolute;font-size:8px;'></span>\n" +
                " <span style='left:196px;top:141px;position:absolute;font-size:8px;'></span>\n" +
                "<span style='left:242px;top:145px;position:absolute;font-size:8px;'>Phone:</span>\n" +
                " <span style='left:250px;top:88px;position:absolute;font-size:8px;'>Account:</span>\n" +
                "\n" +
                "    <span style='left:4px;top:19px;position:absolute;font-size:12px;'>AMAZON FULFILLMENT CENTER</span>\n" +
                " <span style='left:4px;top:30px;position:absolute;font-size:12px;'>PLOT 1, LYONS PARK</span>\n" +
                " <span style='left:4px;top:41px;position:absolute;font-size:12px;'></span>\n" +
                " <span style='left:4px;top:52px;position:absolute;font-size:12px;'>SAYER DR COVENTRY</span>\n" +
                "<span style='left:4px;top:63px;position:absolute;font-size:12px;'></span>\n" +
                "<span style='left:4px;top:74px;position:absolute;font-size:12px;'>CV5 9PF</span>\n" +
                "<span style='left:90px;top:74px;position:absolute;font-size:12px;'>United Kingdom</span>\n" +
                "<span style='left:220px;top:68px;position:absolute;font-size:18px;'></span>\n" +
                "<span style='left:4px;top:88px;position:absolute;font-size:8px;'>Contact</span>\n" +
                "<span style='left:4px;top:120px;position:absolute;font-size:8px;'>Consignment</span>\n" +
                "<span style='left:4px;top:128px;position:absolute;font-size:8px;'>Ref</span>\n" +
                " <span style='left:4px;top:104px;position:absolute;font-size:8px;'>Info</span>\n" +
                "\n" +
                " <span style='left:60px;top:104px;font-size:8px;position:absolute;'></span>\n" +
                " <span style='left:60px;top:112px;font-size:8px;position:absolute;'></span>\n" +
                "\n" +
                " <span style='left:60px;top:88px;position:absolute;font-size:8px;'></span>\n" +
                "<span style='left:60px;top:120px;position:absolute;font-size:8px;'>1862662333</span>\n" +
                "<span style='left:60px;top:128px;position:absolute;font-size:8px;'></span>\n" +
                "<span style='left:60px;top:136px;position:absolute;font-size:8px;'></span>\n" +
                " <span style='left:60px;top:144px;position:absolute;font-size:8px;'></span>\n" +
                "\n" +
                " <span style='left:284px;top:18px;position:absolute;font-size:8px;'>Packages</span>\n" +
                " <span style='left:276px;top:48px;position:absolute;font-size:8px;'>Total Weight</span>\n" +
                " <span style='left:274px;top:26px;position:absolute;font-size:16px;'>2 of 2</span>\n" +
                "<span style='left:274px;top:56px;position:absolute;font-size:16px;'>2 kg</span>\n" +
                "\n" +
                "  <span style='left:6px;top:180px;position:absolute;font-size:8px;'>Track</span>\n" +
                "<span style='left:350px;top:180px;position:absolute;font-size:8px;'>Service</span>\n" +
                "<span style='left:5px;top:158px;position:absolute;font-size:22px;'>1550</span>\n" +
                " <span style='left:62px;top:160px;position:absolute;font-size:16px;'>1862 6623 34D</span>\n" +
                "<span style='left:5px;width:370px;top:250px;position:absolute;font-size:10px;text-align:center;'>      29/11/23 16:01 Web 4.6.10-api      </span>\n" +
                "<span style='left:19px;top:265px;border-top-style:solid;border-width:thin;position:absolute;'><img width='348px' height='95px' src=''/></span>\n" +
                "<span style='left:85px;top:363px;position:absolute;font-size:12px;'>0CV5 9PF1 5501 8626 6233 4812 826T</span>\n" +
                "<span style='right:5px;top:158px;width:250px;text-align:right;position:absolute;font-size:20px;'>          NEXT DAY</span>\n" +
                "<span style='left:130px;top:230px;position:absolute;font-size:14px;'>   812-GB - CV5 9PF   </span>\n" +
                "<span style='left:10px;top:190px;position:absolute;font-size:18px;'></span>\n" +
                "<span style='left:10px;top:225px;position:absolute;font-size:30px;'></span>\n" +
                " <span style='right:10px;top:225px;position:absolute;font-size:30px;'>0065</span>\n" +
                "<span style='left:3px;top:183px;width:380px;font-size:46px;position:absolute;text-align:center;'>GB-CV5 9PF</span>\n" +
                "\n" +
                "               <table style='display:inline-block;border:3px solid black;padding:0;border-spacing:0;border-collapse:collapse;' height='385' width='385px'>\n" +
                "     <tr>\n" +
                "        <td>\n" +
                "             <table style='padding:0;border-spacing:0;border-collapse:collapse;' width='100%'>\n" +
                "                <tr>\n" +
                "                   <td style='font-size:1px;border-bottom:1px solid black;' colspan='4'>\n" +
                "                        <div style='height:10px'>&nbsp;</div>\n" +
                "                     </td>\n" +
                "                 </tr>\n" +
                "                 <tr>\n" +
                "                    <td colspan='2' style='text-align:left;border-bottom:1px solid black;vertical-align:top;'>\n" +
                "                     <div style='height:69px'>&nbsp;</div>\n" +
                "                    </td>\n" +
                "                     <td style='border-bottom:1px solid black;border-left:1px solid black;'>&nbsp;</td>\n" +
                "                    <td style='border-bottom:3px solid black;border-left:1px solid black;' rowspan='2'>&nbsp;</td>\n" +
                "                 </tr>\n" +
                "                 <tr>\n" +
                "                    <td style='border-bottom:3px solid black;'>\n" +
                "                       <div style='height:66px'>&nbsp;</div>\n" +
                "                      </td>\n" +
                "                    <td style='border-bottom:3px solid black;border-left:1px solid black;' colspan='2'>&nbsp;</td>\n" +
                "               </tr>\n" +
                "                <tr>\n" +
                "                    <td style='text-align:left;vertical-align:top;' colspan='4'>\n" +
                "                      <div style='height:226px'>&nbsp;</div>\n" +
                "                 </td>\n" +
                "               </tr>\n" +
                "               <tr>\n" +
                "                  <td style='font-size:1px;' width='195px'>&nbsp;</td>\n" +
                "                   <td style='font-size:1px;' width='70px'>\n" +
                "                        <div style='height:1px'>&nbsp;</div>\n" +
                "                   </td>\n" +
                "                 <td style='font-size:1px;' width='80px'>&nbsp;</td>\n" +
                "                    <td style='font-size:1px;' width='40px'>&nbsp;</td>\n" +
                "                </tr>\n" +
                "            </table>\n" +
                "         </td>\n" +
                "</tr>\n" +
                "</table>\n" +
                "\n" +
                "           </div>\n" +
                "       </td>\n" +
                "     </tr>\n" +
                "</table></div></html>";

        String pdfBase64ByHtml = getPDFBase64ByHtml(str);
        base64ToPDF(pdfBase64ByHtml,"/Users/zengqd/Desktop/临时文件/out1.pdf");
    }


}
